package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallComment;
import com.ctsi.ssdc.domain.LitemallCommentExample;
import com.ctsi.ssdc.service.LitemallCommentService;
import com.ctsi.ssdc.repository.LitemallCommentRepository;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallComment.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallCommentServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallCommentRepository, LitemallComment, Integer, LitemallCommentExample> 
	implements LitemallCommentService {

    @Autowired
    LitemallCommentRepository litemallCommentRepository;



    /**
    * GET  /litemallComments : get the litemallComments firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallComment> findFirstStringColumn(String content,Pageable pageable){
        String str = content;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallCommentExample litemallCommentExample = new LitemallCommentExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallCommentExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallCommentExample.createCriteria();
        } else{
            litemallCommentExample.createCriteria().andContentLike("%" + str +"%");
        }
        List<LitemallComment>  data = litemallCommentRepository.selectByExample(litemallCommentExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallCommentRepository.countByExample(litemallCommentExample);
        }
        return new PageResult<LitemallComment>(data,count,count);
    }

    @Override
    public int save(LitemallComment comment) {
        comment.setAddTime(ZonedDateTime.now());
        comment.setUpdateTime(ZonedDateTime.now());
        return litemallCommentRepository.insertSelective(comment);
    }

    @Override
    public int count(Byte type, Integer valueId, Integer showType) {
        LitemallCommentExample example = new LitemallCommentExample();
        if (showType == 0) {
            example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        } else if (showType == 1) {
            example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(true).andDeletedEqualTo(false);
        } else {
            throw new RuntimeException("showType不支持");
        }
        return (int) litemallCommentRepository.countByExample(example);
    }

    @Override
    public List<LitemallComment> query(Byte type, Integer valueId, Integer showType, Integer page, Integer limit) {
        LitemallCommentExample example = new LitemallCommentExample();
        example.setOrderByClause(LitemallComment.Column.addTime.desc());
        if (showType == 0) {
            example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andDeletedEqualTo(false);
        } else if (showType == 1) {
            example.or().andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(true).andDeletedEqualTo(false);
        } else {
            throw new RuntimeException("showType不支持");
        }
        PageHelper.startPage(page, limit);
        return litemallCommentRepository.selectByExample(example);
    }

    @Override
    public List<LitemallComment> queryGoodsByGid(Integer id, int offset, int limit) {
        LitemallCommentExample example = new LitemallCommentExample();
        example.setOrderByClause(LitemallComment.Column.addTime.desc());
        example.or().andValueIdEqualTo(id).andTypeEqualTo((byte) 0).andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        return litemallCommentRepository.selectByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
